package com.zy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.entity.ZyRole;
import com.zy.entity.ZyUser;
import com.zy.mapper.ZyRoleMapper;
import com.zy.mapper.ZyUserMapper;
import com.zy.service.impl.ZyUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class ZyUserController {
    //是否允许注册
    public static boolean isRegister1 = false;
    public static boolean isRegister2 = false;
    public static boolean isRegister3 = false;
    @Autowired
    ZyUserServiceImpl zyUserService;
    @Autowired
    ZyUserMapper zyUserMapper;
    @Autowired
    ZyRoleMapper zyRoleMapper;

    /**
     * 注册
     *
     * @param username
     * @return
     */
    //用户名唯一
    @GetMapping("/register1")
    @ResponseBody
    public String addZyUser1(String username) {
        if (zyUserMapper.getUserByUserName(username) != null) {
            isRegister1 = false;
            return "用户名不可用！";
        } else {
            isRegister1 = true;
        }
        return "";
    }

    //号码唯一
    @GetMapping("/register2")
    @ResponseBody
    public String addZyUser2(String phone) {
        if (zyUserMapper.getPhoneByPhone(phone) != null) {
            isRegister2 = false;
            return "号码不可用！";
        } else {
            isRegister2 = true;
        }
        return "";
    }

    //邮箱唯一
    @GetMapping("/register3")
    @ResponseBody
    public String addZyUser3(String email) {
        if (zyUserMapper.getEmailByEmail(email) != null) {
            isRegister3 = false;
            return "邮箱不可用！";
        } else {
            isRegister3 = true;
        }
        return "";
    }

    @PostMapping("/register")
    public ModelAndView register(String username, String password, String email, String phone) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");

        //检查是否允许注册
        if (!isRegister1 || !isRegister2 || !isRegister3) {
            return modelAndView; //不允许注册
        }

        //编码器,密码加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        ZyUser zyUser = new ZyUser();
        zyUser.setUsername(username);
        zyUser.setPassword(passwordEncoder.encode(password));
        zyUser.setPhone(phone);
        zyUser.setEmail(email);
        zyUserService.save(zyUser);

        return modelAndView;
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView getUsers(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                 @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize) {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");

        //user分页
        IPage<ZyUser> userPage = new Page<>(pageNo, pageSize);
        IPage<ZyUser> users = zyUserMapper.selectPage(userPage, null);

        //查询所有角色
        List<ZyRole> roles = zyRoleMapper.selectList(null);

        modelAndView.addObject("users", users);
        modelAndView.addObject("roles", roles);

        return modelAndView;
    }

    /**
     * 判断是否已登录
     *
     * @param authentication
     * @return
     */
    @RequestMapping("/")
    public String index(Authentication authentication) {

        try {
            String username = authentication.getName();
        } catch (NullPointerException e) {
            return "index";
        }

        return "redirect:/menu";
    }

    /**
     * 登录失败,错误消息回显
     */
    @RequestMapping("/loginError")
    public ModelAndView loginError() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        modelAndView.addObject("loginError", "用户名或密码错误！");

        return modelAndView;
    }

}
